<template>
  <!-- 详情 -->
  <div class="item-container" v-if="status == 'view'">
    <a-space :size="22" direction="vertical" fill>
      <basic-info-summary-panel />
      <basic-info-descriptions title="企业信息" :data="basicInfo">
        <template #business-qualification>
          <div class="flex">
            <a-space :size="16">
              <UploadImg disabled :single="true" :modelValue="enterpriseDetail.businessLicenseFileId" title="营业执照" />
              <UploadImg disabled :single="true" :modelValue="enterpriseDetail.leaseQualificationFileId" title="租赁资质" />
            </a-space>
          </div>
        </template>
      </basic-info-descriptions>
      <basic-info-descriptions title="企业负责人" :data="headMaster" />
      <basic-info-descriptions title="其他联系人" :data="otherContacts" />
    </a-space>
  </div>
  <!-- 编辑 -->
  <div class="item-container" v-else>
    <basic-info-edit ref="basicInfoFormRef" :enterpriseDetail="enterpriseDetail" />
  </div>
</template>

<script setup name="EnterpriseBasicInfo">
  import basicInfoSummaryPanel from './basic-info-summary-panel.vue'
  import basicInfoDescriptions from './basic-info-descriptions.vue'
  import basicInfoEdit from './basic-info-edit.vue'
  import dayjs from 'dayjs'
  import { ref, computed } from 'vue'
  const props = defineProps({
    status: String,
    enterpriseDetail: Object,
  })
  const emit = defineEmits(['updateData'])
  // 企业信息
  const basicInfo = computed(() => {
    const eVal = props.enterpriseDetail
    return [
      {
        label: '企业名称',
        value: eVal?.epName || '-',
      },
      {
        label: '注册地址',
        value: eVal?.epAddress || '-',
      },
      {
        label: '车辆规模',
        value: eVal?.vehicleScale ? eVal?.vehicleScale + ' 辆' : '-',
      },
      {
        label: '统一社会信用代码',
        value: eVal?.uniformSocialCreditCode || '-',
      },
      {
        label: '法人姓名',
        value: eVal?.epLegalPerson || '-',
      },
      {
        label: '法人身份证号',
        value: eVal?.epLegalPersonIdCard || '-',
      },
      {
        label: '营业执照有效期',
        render: () => {
          if (eVal) {
            if (eVal?.businessLicenseStartDate) {
              let start = eVal.businessLicenseStartDate
              if (start) {
                let end = eVal.businessLicenseEndDate
                if (end) {
                  end = dayjs(end).format('YYYY年MM月DD日')
                } else {
                  end = '长期'
                }
                return dayjs(start).format('YYYY年MM月DD日') + ' 至 ' + end
              } else {
                return '-'
              }
            }
          }
          return '-'
        },
        span: 3,
      },
      {
        label: '证件资质',
        span: 3,
        slotName: 'business-qualification',
      },
    ]
  })
  // 负责人信息
  const headMaster = computed(() => {
    const eVal = props.enterpriseDetail
    return [
      {
        label: '负责人姓名',
        value: eVal?.epDirector || '-',
      },
      {
        label: '负责人手机号',
        value: eVal?.epPhoneNo || '-',
      },
      {
        label: '负责人邮箱',
        value: eVal?.epEmail || '-',
      },
    ]
  })
  // 其他联系人
  const otherContacts = computed(() => {
    const eVal = props.enterpriseDetail
    const epContacts = eVal?.epContacts || []
    let arr = []
    epContacts.forEach((contactItem) => {
      const { contactJob, contactName, contactPhone } = contactItem
      arr.push({ label: '职务', value: contactJob || '-' })
      arr.push({ label: '姓名', value: contactName || '-' })
      arr.push({ label: '手机号', value: contactPhone || '-' })
    })
    return arr
  })

  const basicInfoFormRef = ref()
  const saveFn = () => {
    return new Promise((resolve, reject) => {
      basicInfoFormRef.value
        .saveFn()
        .then(() => {
          emit('updateData')
          resolve()
        })
        .catch(() => {
          reject()
        })
    })
  }

  defineExpose({
    saveFn,
  })
</script>

<style lang="less" scoped>
  .descriptions-title {
    font-size: 16px;
    font-weight: 600;
    color: #1d2129;
  }
</style>
